<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Adv;
use app\common\model\Banner;
use app\common\model\Cate;
use app\common\model\Quotation as QuotationModel;

class Quotation
{
    //程宇
    //获取行情大厅
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取banner
            'cate' => $this->cate(),//获取分类
            'adv' => $this->adv(),//获取广告位
        ]);
    }

    /**
     * 获取banner
     */
    public function banner()
    {
        $model = New Banner();
        $banner = $model->where(['status' => 1, 'type' => 4])
            ->field('id,title,image')
            ->limit(0, 1)
            ->select();
        foreach ($banner as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $banner;
    }

    /**
     * 获取分类
     */
    public function cate()
    {
        $model = New Cate();
        $cate = $model->where(['status' => 1, 'type' => 6])
            ->field('id,name')
            ->limit(0, 6)
            ->select();
        return $cate;
    }

    /**
     * 获取广告位
     */
    public function adv()
    {
        $model = New Adv();
        $adv = $model->where(['status' => 1, 'type' => 3])
            ->field('id,image,title')
            ->limit(0, 1)
            ->find();
        $adv['image'] = env('app.yuming') . $adv['image'];
        return $adv;
    }

    /**
     * 获取行情
     */
    public function quotation()
    {
        $id = input('id/d');
        $page = input('page/d', 1);
        $pagesize = 10;
        $model = New QuotationModel();
        if (empty($id)) {
            $quotation = $model->where('status', 1)
                ->field('id,qushi,name,address,price,up_down,created_at')
                ->page($page, $pagesize)
                ->order('sort', 'asc')
                ->select();
            foreach ($quotation as $v) {
                $v['created_at'] = date('Y-m-d', strtotime($v['created_at']));
            }
            $count = count($model->where('status', 1)->select());
            $maxpage = ceil($count / $pagesize);
            return sucessMsg('200', '获取成功', ['quotation' => $quotation, 'count' => $count, 'page' => $page, 'maxpage' => $maxpage, 'pagesize' => $pagesize]);
        } else {
            $quotation = $model->where(['status' => 1, 'cate_id' => $id])
                ->field('id,qushi,name,address,price,up_down,created_at')
                ->page($page, $pagesize)
                ->order('sort', 'asc')
                ->select();
            foreach ($quotation as $v) {
                $v['created_at'] = date('Y-m-d', strtotime($v['created_at']));
            }
            $count = count($model->where(['status' => 1, 'cate_id' => $id])->select());
            $maxpage = ceil($count / $pagesize);
            return sucessMsg('200', '获取成功', ['quotation' => $quotation, 'page' => $page, 'maxpage' => $maxpage, 'count' => $count, 'pagesize' => $pagesize]);
        }
    }

    public function content()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg('400', '获取失败，请传入id');
        }
        $model = New QuotationModel();
        $content = $model->where(['status' => 1, 'id' => $id])
            ->field('id,image,name,qushi,price')
            ->find();
        if (empty($content)) {
            return errMsg('400', '获取失败，暂无数据');
        }
        $content['image'] = env('app.yuming') . $content['image'];

        $quotation = $model->where('status', 1)
            ->field('id,qushi,name,address,price,up_down,created_at')
            ->order('sort', 'asc')
            ->limit(0, 2)
            ->select();
        //dd($quotation);
        foreach ($quotation as $v) {
            $v['created_at'] = date('Y-m-d', strtotime($v['created_at']));
        }
        return sucessMsg('200', '获取成功', ['content' => $content, 'quotation' => $quotation]);


    }

}
